package indi.zhang.webbatch.mysql.schedule;

import indi.zhang.webbatch.mysql.batch.BatchSimple;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;

@Component
@Slf4j
public class Timer {

    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private JobLocator jobLocator;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void createBsInvestigateTask() {
        try {
            Job job = jobLocator.getJob(BatchSimple.JOB);
            jobLauncher.run(job, new JobParametersBuilder()
                            .addDate("date",new Date())
                            .addString("data", LocalDate.now().toString())
                    .toJobParameters());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("任务[articleJob]启动失败，错误信息:{}", e.getMessage());
        }
    }

}
